#this is module for vmc configure
from xml.dom import minidom
import os
import yaml


class ConfigureVMC(object):
    def __init__(self,filename):
        self.__imageRoot = "/data/images"
        self.__vmxmlRoot = "/data/xml"
        self.__networkRoot = "/data/network"
        self.__rootfsRoot = "/data/rootfs"
        self.__nfsmountRoot = "/data/nfsmount"
        self.__port = "8989"
        self.__configureFilename = filename
    def configure(self):
        #if self.configureFilename don't exist return -1
        if not os.path.isfile(self.__configureFilename):
            print "%s not exist, use default configration:"%self.__configureFilename
            print "\t image path:    /data/images"
            print "\t xml path:      /data/xml"
            print "\t network path:  /data/network"
            print "\t rootfs path:  /data/rootfs"
            print "\t nfsmount path: /data/nfsmount"
            print "\t port:  8989"
            conf = open("/etc/vmc.yaml","w")
            content = ["vmc:\n",\
                       "  port: 2212\n",\
                       "  image_root: /data/images\n",\
                       "  vmxml_root: /data/xml\n",\
                       "  network_root: /data/network\n",\
                       "  rootfs_root: /data/rootfs",\
                       "  nfsmount_root: /data/nfsmount",\
                       "  pem_name: 123.pem\n",\
                       "  cert_path: /root\n",\
                       "  monitor_send_path: https://dev.ivic.org.cn/vmcs/api\n"]
            conf.writelines(content)
            conf.close()
            return -1
        try:
            config_file = yaml.load(file(self.__configureFilename, 'r'))
            self.__imageRoot = config_file['vmc']['image_root']
            self.__vmxmlRoot = config_file['vmc']['vmxml_root']
            self.__port = config_file['vmc']['port']
            self.__networkRoot = config_file['vmc']['network_root']
            self.__rootfsRoot = config_file['vmc']['rootfs_root']
            self.__nfsmountRoot = config_file['vmc']['nfsmount_root']
        except Exception:
            print "some error happen when configure parse"
            return -2;
        return 0
    def __str__(self):
        return 'imageRoot=%s\nvmxml=Root%s\nport=%s\nnetworkRoot=%s\nrootfsRoot=%s'%(self.__imageRoot,self.__vmxmlRoot,self.__port,self.__networkRoot,self.__rootfsRoot);
    
    def getConf(self):
        return [self.__imageRoot,self.__vmxmlRoot,self.__port,self.__networkRoot,self.__rootfsRoot];
    
    __repr__ = __str__
#this part is a test
if __name__ == "__main__":
    conf = ConfigureVMC("vmc.yaml")
    conf.configure()
    print conf.getConf()
    print conf
